package com.zenithmind.classroom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.classroom.pojo.domain.LearningProgress;
import com.zenithmind.classroom.pojo.vo.LearningAnalysisVO;
import com.zenithmind.classroom.pojo.vo.LearningProgressVO;
import com.zenithmind.classroom.pojo.vo.LearningStatisticsVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 学习进度Mapper接口
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface LearningProgressMapper extends BaseMapper<LearningProgress> {

    /**
     * 获取用户课程进度
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 学习进度
     */
    LearningProgressVO selectUserCourseProgress(@Param("userId") String userId, @Param("courseId") String courseId);

    /**
     * 获取用户章节进度
     * 
     * @param userId 用户ID
     * @param chapterId 章节ID
     * @return 学习进度
     */
    LearningProgressVO selectUserChapterProgress(@Param("userId") String userId, @Param("chapterId") String chapterId);

    /**
     * 获取用户课时进度
     * 
     * @param userId 用户ID
     * @param lessonId 课时ID
     * @return 学习进度
     */
    LearningProgressVO selectUserLessonProgress(@Param("userId") String userId, @Param("lessonId") String lessonId);

    /**
     * 获取用户学习统计
     * 
     * @param userId 用户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 学习统计
     */
    LearningStatisticsVO selectUserLearningStatistics(@Param("userId") String userId, 
                                                     @Param("startTime") LocalDateTime startTime, 
                                                     @Param("endTime") LocalDateTime endTime);

    /**
     * 获取课程学习统计
     * 
     * @param courseId 课程ID
     * @return 学习统计
     */
    LearningStatisticsVO selectCourseLearningStatistics(@Param("courseId") String courseId);

    /**
     * 获取用户学习分析
     * 
     * @param userId 用户ID
     * @param days 分析天数
     * @return 学习分析
     */
    LearningAnalysisVO selectUserLearningAnalysis(@Param("userId") String userId, @Param("days") Integer days);

    /**
     * 获取学习排行榜
     * 
     * @param courseId 课程ID
     * @param type 排行类型（1-学习时长，2-完成进度，3-学习质量）
     * @param limit 限制数量
     * @return 排行榜
     */
    List<LearningProgressVO> selectLearningRanking(@Param("courseId") String courseId, 
                                                  @Param("type") Integer type, 
                                                  @Param("limit") Integer limit);

    /**
     * 获取学习建议
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 学习建议
     */
    List<String> selectLearningRecommendations(@Param("userId") String userId, @Param("courseId") String courseId);

    /**
     * 记录学习行为
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @param lessonId 课时ID
     * @param action 行为类型
     * @param duration 持续时间
     * @param deviceType 设备类型
     * @return 影响行数
     */
    int recordLearningBehavior(@Param("userId") String userId, 
                              @Param("courseId") String courseId, 
                              @Param("lessonId") String lessonId, 
                              @Param("action") String action, 
                              @Param("duration") Integer duration, 
                              @Param("deviceType") Integer deviceType);

    /**
     * 获取学习路径
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 学习路径
     */
    List<LearningProgressVO> selectLearningPath(@Param("userId") String userId, @Param("courseId") String courseId);

    /**
     * 预测学习完成时间
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 预计完成时间
     */
    LocalDateTime predictCompletionTime(@Param("userId") String userId, @Param("courseId") String courseId);

    /**
     * 获取学习热力图数据
     * 
     * @param userId 用户ID
     * @param year 年份
     * @return 热力图数据
     */
    List<LearningAnalysisVO.HeatmapData> selectLearningHeatmap(@Param("userId") String userId, @Param("year") Integer year);

    /**
     * 同步学习进度
     * 
     * @param userId 用户ID
     * @param deviceType 设备类型
     * @return 影响行数
     */
    int syncLearningProgress(@Param("userId") String userId, @Param("deviceType") Integer deviceType);

    /**
     * 重置学习进度
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 影响行数
     */
    @Update("UPDATE learning_progress SET " +
            "status = 0, " +
            "progress_percentage = 0, " +
            "studied_seconds = 0, " +
            "last_position = 0, " +
            "completion_time = NULL, " +
            "update_time = NOW() " +
            "WHERE user_id = #{userId} AND course_id = #{courseId}")
    int resetLearningProgress(@Param("userId") String userId, @Param("courseId") String courseId);

    /**
     * 获取学习提醒
     * 
     * @param userId 用户ID
     * @return 提醒列表
     */
    List<String> selectLearningReminders(@Param("userId") String userId);

    /**
     * 更新学习进度
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @param chapterId 章节ID
     * @param lessonId 课时ID
     * @param status 状态
     * @param progressPercentage 进度百分比
     * @param studiedSeconds 学习时长
     * @param lastPosition 最后位置
     * @param deviceType 设备类型
     * @return 影响行数
     */
    int updateProgress(@Param("userId") String userId, 
                      @Param("courseId") String courseId, 
                      @Param("chapterId") String chapterId, 
                      @Param("lessonId") String lessonId, 
                      @Param("status") Integer status, 
                      @Param("progressPercentage") Integer progressPercentage, 
                      @Param("studiedSeconds") Integer studiedSeconds, 
                      @Param("lastPosition") Integer lastPosition, 
                      @Param("deviceType") Integer deviceType);

    /**
     * 开始学习
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @param chapterId 章节ID
     * @param lessonId 课时ID
     * @param deviceType 设备类型
     * @return 影响行数
     */
    int startLearning(@Param("userId") String userId, 
                     @Param("courseId") String courseId, 
                     @Param("chapterId") String chapterId, 
                     @Param("lessonId") String lessonId, 
                     @Param("deviceType") Integer deviceType);

    /**
     * 完成学习
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @param chapterId 章节ID
     * @param lessonId 课时ID
     * @return 影响行数
     */
    @Update("UPDATE learning_progress SET " +
            "status = 2, " +
            "progress_percentage = 100, " +
            "completion_time = NOW(), " +
            "update_time = NOW() " +
            "WHERE user_id = #{userId} AND course_id = #{courseId} " +
            "AND (#{chapterId} IS NULL OR chapter_id = #{chapterId}) " +
            "AND (#{lessonId} IS NULL OR lesson_id = #{lessonId})")
    int completeLearning(@Param("userId") String userId, 
                        @Param("courseId") String courseId, 
                        @Param("chapterId") String chapterId, 
                        @Param("lessonId") String lessonId);

    /**
     * 暂停学习
     * 
     * @param userId 用户ID
     * @param courseId 课程ID
     * @param chapterId 章节ID
     * @param lessonId 课时ID
     * @return 影响行数
     */
    @Update("UPDATE learning_progress SET " +
            "status = 3, " +
            "update_time = NOW() " +
            "WHERE user_id = #{userId} AND course_id = #{courseId} " +
            "AND (#{chapterId} IS NULL OR chapter_id = #{chapterId}) " +
            "AND (#{lessonId} IS NULL OR lesson_id = #{lessonId})")
    int pauseLearning(@Param("userId") String userId, 
                     @Param("courseId") String courseId, 
                     @Param("chapterId") String chapterId, 
                     @Param("lessonId") String lessonId);

    /**
     * 获取用户学习连续天数
     * 
     * @param userId 用户ID
     * @return 连续天数
     */
    @Select("SELECT COALESCE(MAX(continuous_days), 0) FROM user_learning_statistics WHERE user_id = #{userId}")
    Integer getUserContinuousDays(@Param("userId") String userId);

    /**
     * 获取用户总学习时长
     * 
     * @param userId 用户ID
     * @return 总时长（分钟）
     */
    @Select("SELECT COALESCE(SUM(studied_seconds), 0) / 60 FROM learning_progress WHERE user_id = #{userId} AND deleted = 0")
    Integer getUserTotalStudyMinutes(@Param("userId") String userId);

    /**
     * 获取用户学习课程数
     * 
     * @param userId 用户ID
     * @return 课程数
     */
    @Select("SELECT COUNT(DISTINCT course_id) FROM learning_progress WHERE user_id = #{userId} AND deleted = 0")
    Integer getUserStudyCourseCount(@Param("userId") String userId);

    /**
     * 获取用户完成课程数
     * 
     * @param userId 用户ID
     * @return 完成课程数
     */
    @Select("SELECT COUNT(DISTINCT course_id) FROM learning_progress WHERE user_id = #{userId} AND status = 2 AND deleted = 0")
    Integer getUserCompletedCourseCount(@Param("userId") String userId);
}
